/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerPeer;

import common.function_common;
import common.global;
import java.io.*;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JList;

/**
 *
 * @author DongVan
 */
public class NodeMap {

    private JList m_ListGUI;
    private ArrayList<Node> m_ListNode;
    private DefaultListModel listModel;

    public NodeMap(JList listGUI) {
        this.m_ListGUI = listGUI;
    }

    public ArrayList<Node> getM_ListNode() {
        return m_ListNode;
    }

    public void setM_ListNode(ArrayList<Node> m_ListNode) {
        this.m_ListNode = m_ListNode;
    }

    public void readFilePeers() {
        if (this.m_ListNode == null) {
            this.m_ListNode = new ArrayList<Node>();
        } else {
            this.m_ListNode.clear();
        }
        try {
            BufferedReader input = new BufferedReader(new FileReader("nodeMap\\nodeMap.txt"));
            String line = "";
//            line = input.readLine();
            while ((line = input.readLine()) != null) {
                String[] str = line.split("-");
                Node node = new Node();
                node.setM_ID(Integer.parseInt(str[0]));
                node.setM_IdAddress(str[1]);
                node.setM_Port(Integer.parseInt(str[2]));
                this.m_ListNode.add(node);
            }
            input.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("dọc file node bị lỗi");
        }
    }

    private boolean checkIP(Node newPeer) {
        for (int i = 0; i < this.m_ListNode.size(); i++) {
            Node node = new Node();
            node = this.m_ListNode.get(i);
            if (node.getM_IdAddress().equals(newPeer.getM_IdAddress())) {
                System.out.println("Adress " + newPeer.getM_IdAddress() + " da ton tai!");
                return false;
            }
        }
        return true;
    }

    public void updateFilePeers(Node peer) {
        if (checkIP(peer)) {
            try {
                m_ListNode.add(peer);
                BufferedWriter output = new BufferedWriter(new FileWriter("nodeMap\\nodeMap.txt"));
                String line = "";
                for (int i = 1; i <= this.m_ListNode.size(); i++) {
                    Node node = new Node();
                    node = this.m_ListNode.get(i - 1);
                    node.setM_ID(i);
                    line = node.getM_ID() + "-" + node.getM_IdAddress() + "-" + node.getM_Port();
                    output.write(line);
                    output.newLine();
                }
                output.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            this.updateListPeerOntoGUI();
        }
    }

    public void updateListPeerOntoGUI() {
        this.readFilePeers();// đọc file nodemap.txt chứa thông tin các peer
        if (getListModel() == null) {
            setListModel(new DefaultListModel());
            this.m_ListGUI.setModel(getListModel());
        } else {
            getListModel().clear();
        }
        for (Node item : this.getM_ListNode()) {
            getListModel().addElement(item.pcInformation());
        }
    }

    /**
     * @return the m_ListGUI
     */
    public JList getM_ListGUI() {
        return m_ListGUI;
    }

    /**
     * @param m_ListGUI the m_ListGUI to set
     */
    public void setM_ListGUI(JList m_ListGUI) {
        this.m_ListGUI = m_ListGUI;
    }

    /**
     * @return the listModel
     */
    public DefaultListModel getListModel() {
        return listModel;
    }

    /**
     * @param listModel the listModel to set
     */
    public void setListModel(DefaultListModel listModel) {
        this.listModel = listModel;
    }
}
